/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package uber.netbsd.community.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;
import uber.netbsd.community.dao.comment.CommentManager;

/**
 * Stripes Action to delete users comments
 * @author alex
 */
@UrlBinding("/action/DeleteComment")
public class DeleteCommentActionBean extends BaseActionBean {

    /**
     * Where to jump (url) after operation
     */
    @Validate(required = true)
    private String backUrl;
    /**
     * comment's id
     */
    private Integer commentId;

    public String getBackUrl() {
        return backUrl;
    }

    public void setBackUrl(String backUrl) {
        this.backUrl = backUrl;
    }

    public Integer getCommentId() {
        return commentId;
    }

    public void setCommentId(Integer commentId) {
        this.commentId = commentId;
    }

    @ValidationMethod
    public void validate(ValidationErrors errors) {

        if (CommentManager.getInstance().getComment(commentId) == null) {
            errors.add("global", new SimpleError("comment NOT FOUND"));
        }

    }

    /**
     * User tried to delete comment
     * @return redirect url
     */
    @DefaultHandler
    public Resolution deleteComment() {

        CommentManager.getInstance().delete(commentId);
      
        return new RedirectResolution(backUrl);

    }
}
